﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

using QuDao;
using FineUICore;
using Kalman;
using Kalman.Extensions;
using Microsoft.AspNetCore.Http;
using AliyunOss;

namespace AdminUI.Pages.BaseData
{
    public partial class AlbumAddModel : BaseModel
    {
        readonly AliyunOssClient oss;
        public AlbumAddModel(AliyunOssClient ossClient)
        {
            oss = ossClient;
        }

        public void OnGet(int rid)
        {
            GoodsID = rid;
        }

        public int GoodsID { get; set; }

        public IActionResult OnPostBtnSubmit_Click(AlbumAddDto dto, IFormFile imageUrl)
        {
            try
            {
                var img = UploadImage(oss, imageUrl);
                if (img == null) throw new Exception("无效的图片文件");

                dto.ImageUrl = img.OssUrl;
                dto.Width = img.Width;
                dto.Height = img.Height;
                dto.FileSize = (int)img.FileSize;

                var id = AlbumBO.Instance.Add(dto);
                //LogAction(ActionType.新增, $"新增相册图片：{id}-{dto.AlbumType}-{dto.Title}");

                FineUICore.PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
                //ActiveWindow.HidePostBack();
            }
            catch (Exception ex)
            {
                var msg = ex.Message;
                if (!(ex is AppException && ex.InnerException == null))
                {
                    msg = ex.ToString();
                }

                Alert.Show(msg, "错误信息", MessageBoxIcon.Error);
            }

            return UIHelper.Result();
        }

    }
}